//
//  GenericLoansCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/9.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class GenericLoansCell: UITableViewCell {
    
    var model : GenericLoansModel! {
        didSet{
            updateData()
        }
    }

    let fontSize = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
    
    fileprivate let typeLabel = UILabel() //类型
    fileprivate let newImageView = UIImageView() //图片
    fileprivate let titleLabel = UILabel() //标题
    fileprivate let linesLabel = UILabel() //额度
    fileprivate let interestLabel = UILabel() //利息
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        typeLabel.font = fontSize
        self.contentView.addSubview(typeLabel)
        
        self.addSubview(newImageView)
        
        titleLabel.textColor = UIColor.gray
        titleLabel.font = fontSize
        self.contentView.addSubview(titleLabel)
        
        linesLabel.textColor = UIColor.gray
        linesLabel.font = fontSize
        self.contentView.addSubview(linesLabel)
        
        let dailyLabel = UILabel()
        dailyLabel.text = dailyInterest + "："
        dailyLabel.font = fontSize
        dailyLabel.textColor = UIColor.gray
        self.contentView.addSubview(dailyLabel)
        
        let millionLabel = UILabel()
        millionLabel.text = "/" + millionYuan
        millionLabel.textColor = UIColor.gray
        millionLabel.font = fontSize
        self.contentView.addSubview(millionLabel)
        
        interestLabel.textColor = UIColor.red
        interestLabel.font = fontSize
        self.contentView.addSubview(interestLabel)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.contentView.addSubview(lineView)
        
        typeLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        newImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(typeLabel.snp.bottom)
            make.left.equalTo(typeLabel)
            make.width.equalTo(RATIO(maxNum: 90))
            make.height.equalTo(RATIO_H(maxNum: 90))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(newImageView)
            make.left.equalTo(newImageView.snp.right).offset(RATIO(maxNum: 10))
            make.right.equalTo(typeLabel)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        
        linesLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.height.equalTo(titleLabel)
            
        }
        
        dailyLabel.snp.makeConstraints { (make) in
            make.top.equalTo(linesLabel.snp.bottom)
            make.left.height.equalTo(linesLabel)
            make.width.greaterThanOrEqualTo(10)
        }
        
        interestLabel.snp.makeConstraints { (make) in
            make.top.height.equalTo(dailyLabel)
            make.left.equalTo(dailyLabel.snp.right)
            make.width.greaterThanOrEqualTo(10)
        }
        
        millionLabel.snp.makeConstraints { (make) in
            make.left.equalTo(interestLabel.snp.right)
            make.top.height.equalTo(interestLabel)
            make.width.greaterThanOrEqualTo(10)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        
    }
    
    //MARK: 更新数据
    fileprivate func updateData() {
        
        typeLabel.text = model.organization_name! + "-" + model.product_name!
        
        newImageView.image = UIImage.imageWithName(KMTool.selectBankImage(bankName: model.organization_name!))
        titleLabel.text = model.time_advange! + "," + model.mortgage! + "," + canLent + model.money! + millionYuan
        linesLabel.text = model.identitychoose
        
        var interest_rate:Double = 0
        if model.rate_unit == years {
            interest_rate = Double(model.interest_rate!)!/365 * 100
        }else if model.rate_unit == month {
            interest_rate = Double(model.interest_rate!)!/30 * 100
        }else{
            interest_rate = Double(model.interest_rate!)!
        }
        
        interestLabel.text = "\(String(format: "%.2f", interest_rate))" + yuan
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

}
